home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / AccessControlContext.java next >
Encoding:
Java Source  |  1999-05-28  |  12.7 KB  |  482 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)AccessControlContext.java    1.22 98/07/20
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.util.Vector;
  18. import sun.security.util.Debug;
  19.  
  20. /** 
  21.  * An AccessControlContext is used to make system resource access decisions
  22.  * based on the context it encapsulates.
  23.  * 
  24.  * <p>More specifically, it encapsulates a context and
  25.  * has a single method, <code>checkPermission</code>,
  26.  * that is equivalent to the <code>checkPermission</code> method
  27.  * in the AccessController class, with one difference: The AccessControlContext
  28.  * <code>checkPermission</code> method makes access decisions based on the 
  29.  * context it encapsulates,
  30.  * rather than that of the current execution thread.
  31.  * 
  32.  * <p>Thus, the purpose of AccessControlContext is for those situations where
  33.  * a security check that should be made within a given context
  34.  * actually needs to be done from within a
  35.  * <i>different</i> context (for example, from within a worker thread).
  36.  * 
  37.  * <p> An AccessControlContext is created by calling the 
  38.  * <code>AccessController.getContext</code> method. 
  39.  * The <code>getContext</code> method takes a "snapshot"
  40.  * of the current calling context, and places
  41.  * it in an AccessControlContext object, which it returns. A sample call is
  42.  * the following:
  43.  * 
  44.  * <pre>
  45.  * 
  46.  *   AccessControlContext acc = AccessController.getContext()
  47.  * 
  48.  * </pre>
  49.  * 
  50.  * <p>
  51.  * Code within a different context can subsequently call the
  52.  * <code>checkPermission</code> method on the
  53.  * previously-saved AccessControlContext object. A sample call is the
  54.  * following:
  55.  * 
  56.  * <pre>
  57.  * 
  58.  *   acc.checkPermission(permission)
  59.  * 
  60.  * </pre> 
  61.  * 
  62.  * @see AccessController
  63.  *
  64.  * @author Roland Schemers
  65.  */
  66.  
  67. public final class AccessControlContext {
  68.  
  69.     private ProtectionDomain context[];
  70.     private boolean isPrivileged;
  71.     private AccessControlContext privilegedContext;
  72.  
  73.     private static boolean debugInit = false;
  74.     private static Debug debug = null;
  75.  
  76.     static Debug getDebug()
  77.     {
  78.     if (debugInit)
  79.         return debug;
  80.     else {
  81.         if (Policy.isSet()) {
  82.         debug = Debug.getInstance("access");
  83.         debugInit = true;
  84.         }
  85.         return debug;
  86.     }
  87.     }
  88.  
  89.     /**
  90.      * Create an AccessControlContext with the given set of ProtectionDomains.
  91.      * Context must not be null. Duplicate domains will be removed from the
  92.      * context.
  93.      *
  94.      * @param context the ProtectionDomains associated with this context.
  95.      */
  96.  
  97.     public AccessControlContext(ProtectionDomain context[])
  98.     {
  99.     if (context.length == 1) {
  100.         this.context = (ProtectionDomain[])context.clone();
  101.     } else {
  102.         Vector v = new Vector(context.length);
  103.         for (int i =0; i< context.length; i++) {
  104.         if ((context[i] != null) &&  (!v.contains(context[i])))
  105.             v.addElement(context[i]);
  106.         }
  107.         this.context = new ProtectionDomain[v.size()];
  108.         v.copyInto(this.context);
  109.     }
  110.     }
  111.  
  112.     /**
  113.      * package private constructor for AccessController.getContext()
  114.      */
  115.  
  116.     AccessControlContext(ProtectionDomain context[], 
  117.                  boolean isPrivileged)
  118.     {
  119.     this.context = context;
  120.     this.isPrivileged = isPrivileged;
  121.     }
  122.  
  123.     /**
  124.      * Returns true if this context is privileged.
  125.      */
  126.     boolean isPrivileged() 
  127.     {
  128.     return isPrivileged;
  129.  
  130.     }
  131.  
  132.     /** 
  133.      * Determines whether the access request indicated by the
  134.      * specified permission should be allowed or denied, based on
  135.      * the security policy currently in effect, and the context in
  136.      * this object.
  137.      * <p>
  138.      * This method quietly returns if the access request
  139.      * is permitted, or throws a suitable AccessControlException otherwise. 
  140.      *
  141.      * @param perm the requested permission.
  142.      * 
  143.      * @exception AccessControlException if the specified permission
  144.      * is not permitted, based on the current security policy and the
  145.      * context encapsulated by this object.
  146.      */
  147.  
  148.     public void checkPermission(Permission perm)
  149.          throws AccessControlException 
  150.     {
  151.     if (getDebug() != null) {
  152.         if (Debug.isOn("stack"))
  153.             Thread.currentThread().dumpStack();
  154.         if (Debug.isOn("domain")) {
  155.         if (context == null) {
  156.             debug.println("domain (context is null)");
  157.         } else {
  158.             for (int i=0; i< context.length; i++) {
  159.             debug.println("domain "+i+" "+context[i]);
  160.             }
  161.         }
  162.         }
  163.     }
  164.  
  165.     /*
  166.      * iterate through the ProtectionDomains in the context.
  167.      * Stop at the first one that doesn't allow the
  168.      * requested permission (throwing an exception).
  169.      *
  170.      */
  171.  
  172.     /* if ctxt is null, all we had on the stack were system domains,
  173.        or the first domain was a Privileged system domain. This
  174.        is to make the common case for system code very fast */
  175.  
  176.     if (context == null)
  177.         return;
  178.  
  179.     for (int i=0; i< context.length; i++) {
  180.         if (context[i] != null &&  !context[i].implies(perm)) {
  181.         if (debug != null) {
  182.             debug.println("access denied "+perm);
  183.             if (Debug.isOn("failure")) {
  184.             Thread.currentThread().dumpStack();
  185.             final ProtectionDomain pd = context[i];
  186.             final Debug db = debug;
  187.             AccessController.doPrivileged (new PrivilegedAction() {
  188.                 public Object run() {
  189.                 db.println("domain that failed "+pd);
  190.                 return null;
  191.                 }
  192.             });
  193.             }
  194.         }
  195.         throw new AccessControlException("access denied "+perm, perm);
  196.         }
  197.     }
  198.  
  199.     // allow if all of them allowed access
  200.     if (debug != null)
  201.         debug.println("access allowed "+perm);
  202.  
  203.     return;    
  204.     }
  205.  
  206.     /**
  207.      * Take the stack-based context (this) and combine it with
  208.      * the privileged context. this method will only be called
  209.      * if privilegedContext is non-null.
  210.      */
  211.     AccessControlContext combineWithPrivilegedContext()
  212.     {
  213.     // this.context could be null if only system code is on the stack
  214.     // in that case, ignore the stack context
  215.     boolean skipStack = (context == null);
  216.  
  217.     AccessControlContext pacc = privilegedContext;
  218.     boolean skipPrivileged = (pacc.context == null);
  219.  
  220.     if (skipPrivileged && skipStack) {
  221.         return this;
  222.     }
  223.  
  224.     int slen = (skipStack) ? 0 : context.length;
  225.  
  226.     // optimization: if the length is less then or equal to two,
  227.     // there is no reason to compress the stack context, it already is
  228.     if (skipPrivileged && slen <= 2)
  229.         return this;
  230.  
  231.     int plen = (skipPrivileged) ? 0 : pacc.context.length;
  232.  
  233.     // optimization: if the length is less then or equal to two,
  234.     // there is no reason to compress the priv context, it already is
  235.     if (skipStack && plen <= 2)
  236.         return pacc;
  237.  
  238.     // optimization: case where we have a length of 1 and
  239.     // protection domains for priv context and stack are equal
  240.     if ((slen == 1) && (plen == 1) && (context[0] == pacc.context[0]))
  241.         return this;
  242.  
  243.     // now we combine both of them, and create a new context.
  244.     ProtectionDomain pd[] = new ProtectionDomain[slen + plen];
  245.  
  246.     int i, j, n;
  247.  
  248.     n = 0;
  249.  
  250.     // first add all the protection domains from the stack context,
  251.     // throwing out nulls and duplicates
  252.  
  253.     if (!skipStack) {
  254.         for (i = 0; i < context.length; i++) {
  255.         boolean add = true;
  256.         for (j= 0; (j < n) && add; j++) {
  257.             add = (context[i] != null) && (context[i] != pd[j]);
  258.         }
  259.         if (add) {
  260.             pd[n++] = context[i];
  261.         }
  262.         }
  263.     }
  264.  
  265.     // now add all the protection domains from the priv context,
  266.     // throwing out nulls and duplicates
  267.  
  268.     if (!skipPrivileged) {
  269.         for (i = 0; i < pacc.context.length; i++) {
  270.         boolean add = true;
  271.         for (j= 0; (j < n) && add; j++) {
  272.             add = (pacc.context[i] != null) &&
  273.             (pacc.context[i] != pd[j]);
  274.         }
  275.         if (add) {
  276.             pd[n++] = pacc.context[i];
  277.         }
  278.         }
  279.     }
  280.  
  281.     // if length isn't equal, we need to shorten the array
  282.     if (n != pd.length) {
  283.         // if all we had were system domains, context is null
  284.         if (n == 0) {
  285.         pd = null;
  286.         } else {
  287.         ProtectionDomain tmp[] = new ProtectionDomain[n];
  288.         System.arraycopy(pd, 0, tmp, 0, n);
  289.         pd = tmp;
  290.         }
  291.     }
  292.  
  293.     return new AccessControlContext(pd, true);
  294.     }
  295.  
  296.  
  297.     /**
  298.      * Take the stack-based context (this) and combine it with
  299.      * the inherited context, if need be.
  300.      */
  301.     AccessControlContext optimize()
  302.     {
  303.     // this.context could be null if only system code is on the stack
  304.     // in that case, ignore the stack context
  305.  
  306.     boolean skipStack = (context == null);
  307.  
  308.     // if this context is privileged, 
  309.     // or if tacc is null, or if tacc.context is null,
  310.     // don't do the thread context
  311.  
  312.     boolean skipThread;
  313.     AccessControlContext tacc;
  314.  
  315.     if (isPrivileged) {
  316.         if (privilegedContext != null)
  317.         return combineWithPrivilegedContext();
  318.         else {
  319.         skipThread = true;
  320.         tacc = null;
  321.         }
  322.     } else {
  323.         tacc = AccessController.getInheritedAccessControlContext();
  324.         skipThread = (tacc == null) || (tacc.context == null);
  325.     }
  326.  
  327.     if (skipThread && skipStack) {
  328.         return this;
  329.     }
  330.  
  331.     int slen = (skipStack) ? 0 : context.length;
  332.  
  333.     // optimization: if the length is less then or equal to two,
  334.     // there is no reason to compress the stack context, it already is
  335.     if (skipThread && slen <= 2)
  336.         return this;
  337.  
  338.     int tlen = (skipThread) ? 0 : tacc.context.length;
  339.  
  340.     // optimization: if the length is less then or equal to two,
  341.     // there is no reason to compress the thread context, it already is
  342.     if (skipStack && tlen <= 2)
  343.         return tacc;
  344.  
  345.     // optimization: case where we have a length of 1 and
  346.     // protection domains for thread and stack are equal
  347.     if ((slen == 1) && (tlen == 1) && (context[0] == tacc.context[0]))
  348.         return this;
  349.  
  350.     // now we combine both of them, and create a new context.
  351.     ProtectionDomain pd[] = new ProtectionDomain[slen + tlen];
  352.  
  353.     int i, j, n;
  354.  
  355.     n = 0;
  356.  
  357.     // first add all the protection domains from the stack context,
  358.     // throwing out nulls and duplicates
  359.  
  360.     if (!skipStack) {
  361.         for (i = 0; i < context.length; i++) {
  362.         boolean add = true;
  363.         for (j= 0; (j < n) && add; j++) {
  364.             add = (context[i] != null) && (context[i] != pd[j]);
  365.         }
  366.         if (add) {
  367.             pd[n++] = context[i];
  368.         }
  369.         }
  370.     }
  371.  
  372.     // now add all the protection domains from the inherited context,
  373.     // throwing out nulls and duplicates
  374.  
  375.     // only do if stack context is not privileged, and the thread context
  376.     // is not null.
  377.  
  378.     if (!skipThread) {
  379.         for (i = 0; i < tacc.context.length; i++) {
  380.         boolean add = true;
  381.         for (j= 0; (j < n) && add; j++) {
  382.             add = (tacc.context[i] != null) &&
  383.             (tacc.context[i] != pd[j]);
  384.         }
  385.         if (add) {
  386.             pd[n++] = tacc.context[i];
  387.         }
  388.         }
  389.     }
  390.  
  391.     // if length isn't equal, we need to shorten the array
  392.     if (n != pd.length) {
  393.         // if all we had were system domains, context is null
  394.         if (n == 0) {
  395.         pd = null;
  396.         } else {
  397.         ProtectionDomain tmp[] = new ProtectionDomain[n];
  398.         System.arraycopy(pd, 0, tmp, 0, n);
  399.         pd = tmp;
  400.         }
  401.     }
  402.  
  403.     // the new context is privileged if either one of the contexts
  404.     // was privileged. XXX: is this right?
  405.  
  406.     return new AccessControlContext(pd, isPrivileged || tacc.isPrivileged);
  407.     }
  408.  
  409.     /**
  410.      * Checks two AccessControlContext objects for equality. 
  411.      * Checks that <i>obj</i> is
  412.      * an AccessControlContext and has the same set of ProtectionDomains
  413.      * as this context.
  414.      * <P>
  415.      * @param obj the object we are testing for equality with this object.
  416.      * @return true if <i>obj</i> is an AccessControlContext, and has the 
  417.      * same set of ProtectionDomains as this context, false otherwise.
  418.      */
  419.     public boolean equals(Object obj) {
  420.     if (obj == this)
  421.         return true;
  422.  
  423.     if (! (obj instanceof AccessControlContext))
  424.         return false;
  425.  
  426.     AccessControlContext that = (AccessControlContext) obj;
  427.  
  428.  
  429.     if (context == null) {
  430.         return (that.context == null);
  431.     }
  432.  
  433.     if (that.context == null)
  434.         return false;
  435.  
  436.     boolean match;
  437.     for (int i = 0; i < this.context.length; i++) {
  438.         match = false;
  439.         for (int j = 0; (j < that.context.length) && !match; j++) {
  440.         match = 
  441.             ((this.context[i] == null) && (that.context[j] == null)) ||
  442.             (this.context[i].equals(that.context[j]));
  443.         }
  444.         if (!match) return false;
  445.     }
  446.  
  447.     match = false;
  448.     for (int i = 0; i < that.context.length; i++) {
  449.         match = false;
  450.         for (int j = 0; (j < this.context.length) && !match; j++) {
  451.         match = 
  452.             ((that.context[i] == null) && (this.context[j] == null)) ||
  453.             (that.context[i].equals(this.context[j]));
  454.         }
  455.         if (!match) return false;
  456.     }
  457.     return true;
  458.  
  459.     }
  460.  
  461.     /**
  462.      * Returns the hash code value for this context. The hash code
  463.      * is computed by exclusive or-ing the hash code of all the protection
  464.      * domains in the context together.
  465.      * 
  466.      * @return a hash code value for this context.
  467.      */
  468.  
  469.     public int hashCode() {
  470.     int hashCode = 0;
  471.  
  472.     if (context == null)
  473.         return hashCode;
  474.  
  475.     for (int i =0; i < context.length; i++) {
  476.         if (context[i] != null)
  477.         hashCode ^= context[i].hashCode();
  478.     }
  479.     return hashCode;
  480.     }
  481. }
  482.